<script lang="ts" setup>
//相关推荐
import { ref, watch } from "vue";
import useGetData from "@/store/get";
import { IndexShowData } from "@/api/interface";
import Image from "@/components/Black/Image.vue";

//获取当前文章分类，取出所有分类下的三篇文章，打乱后随机取三篇展示
// 实例化 store
const store = useGetData();

//接受当前文章分类，文章ID
interface Props {
  data: IndexShowData["cat"] | null;
  id: number;
}

const props = defineProps<Props>();

//存储结果
const data = ref();

watch(
  () => props.data,
  async (newVal) => {
    if (newVal) {
      const arr = newVal.map((obj) => obj.id);
      const id = props.id;
      data.value = await store.GetInterrelatedData(arr, id, 3);
    }
  }
);

//点击文章后平滑滚动至顶部
const rollTop = () => {
  window.scrollTo({
    top: 0,
    behavior: "smooth", // 可选，平滑滚动
  });
};

//图片配置
const configImg = {
  small: "thumbnail", //109
  medium: "medium", //195
  large: "medium", //195
};
</script>

<template>
  <aside v-if="props.data">
    <div class="component">
      <div class="article-list component-content">
        <h2 class="article-list__heading">最新消息</h2>
        <!---->
        <div class="article-list__item" v-for="item in data">
          <router-link
            :to="`/single/${item.id}`"
            class="article-list__item__link"
            @click="rollTop"
          >
            <div class="article-list__item__img-wrapper">
              <div class="picture">
                <Image :data="item.image" :config="configImg"> </Image>
              </div>
            </div>
            <div class="article-list__item__content">
              <div class="article-list__item__category">
                <span v-for="key in item.cat">
                  {{ key.name }}
                </span>
              </div>
              <h3 class="article-list__item__title">
                {{ item.title }}
              </h3>
              <div class="article-list__item__date">{{ item.date }}</div>
            </div>
          </router-link>
        </div>
      </div>
    </div>
  </aside>
  <!--待加载-->
  <div v-else>
    <el-skeleton animated :throttle="500">
      <template #template>
        <aside>
          <div class="component">
            <div class="article-list component-content">
              <h2 class="article-list__heading">
                <el-skeleton-item variant="h3" style="width: 15%" />
              </h2>
              <!---->
              <div class="article-list__item" v-for="item in 3" :key="item">
                <div class="article-list__item__link">
                  <div class="article-list__item__img-wrapper">
                    <div class="picture">
                      <el-skeleton-item
                        variant="image"
                        style="width: 100%; height: 100%"
                      />
                    </div>
                  </div>
                  <div class="article-list__item__content">
                    <div class="article-list__item__category">
                      <span>
                        <el-skeleton-item variant="text" style="width: 15%" />
                      </span>
                    </div>
                    <h3 class="article-list__item__title">
                      <el-skeleton-item variant="h3" style="width: 50%" />
                      <el-skeleton-item variant="h3" style="width: 200px" />
                    </h3>
                    <div class="article-list__item__date">
                      <el-skeleton-item variant="text" style="width: 55%" />
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </aside>
      </template>
    </el-skeleton>
  </div>
</template>

<style lang="less" scoped>
.article-list {
  border-top: 1px solid #d2d2d7;
  padding-top: 52px;
}

.article-list__heading {
  font-size: 24px;
  line-height: 1.16667;
  font-weight: 700;
  letter-spacing: 0.009em;
}
.article-list__item {
  margin-top: 0;
  border-bottom: 1px solid #d2d2d7;
  padding-bottom: 32px;
  padding-top: 32px;
}
.article-list__item:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.article-list__item__link {
  color: #1d1d1f;
  display: flex;
}

//左边
.article-list__item__img-wrapper {
  padding-right: 24px;
  width: 171px;
  .picture {
    width: 171px;
    height: 97px;
    display: block;
    font-size: 0;
    border-radius: 10px;
    overflow: hidden;
    z-index: 0;
    position: relative;
    :deep(.el-image) {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
    }
  }
}
//右边
.article-list__item__content {
  display: flex;
  flex-direction: column;
}

.article-list__item__category {
  font-size: 12px;
  line-height: 1.33337;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: #6e6e73;
  margin-bottom: 4px;
  text-transform: uppercase;
  span {
    padding-right: 10px;
  }
}
.article-list__item__title {
  font-size: 19px;
  line-height: 1.21053;
  font-weight: 700;
  letter-spacing: 0.012em;
  color: @font-color-main;
}
.article-list__item__date {
  font-size: 14px;
  line-height: 1.28577;
  font-weight: 600;
  letter-spacing: -0.016em;
  color: @font-color-assist;
  margin-top: 8px;
}
@media only screen and (max-width: 734px) {
  .article-list {
    padding-top: 28px;
  }
  .article-list__item {
    padding-bottom: 28px;
    padding-top: 28px;
  }
  .article-list__item__img-wrapper {
    padding-right: 16px;
    width: 93px;
    .picture {
      border-radius: 12px;
      width: 93px;
    }
  }
}

@media only screen and (max-width: 1068px) {
  .article-list {
    padding-top: 48px;
  }
  .article-list__heading {
    font-size: 21px;
    line-height: 1.19048;
    font-weight: 700;
    letter-spacing: 0.011em;
  }
  .article-list__item__title {
    font-size: 17px;
    line-height: 1.23536;
    font-weight: 700;
    font-family: "SF Pro SC", "SF Pro Text", "SF Pro Icons", "PingFang SC",
      "Helvetica Neue", "Helvetica", "Arial", sans-serif;
  }
}
</style>
